package com.kevin.config;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.kevin.model.config.PropertiesModel;

@Configuration
public class UploadConfig extends WebMvcConfigurerAdapter {

    /**
     * 增加本地上传路径的支持
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String uploadPath = PropertiesModel.CONFIG.getString("uploadFilePath", "");
        String uploadFileDirPrefix = PropertiesModel.CONFIG.getString("uploadFileDirPrefix", "");
        if (StringUtils.isNotBlank(uploadPath)) {
            if (!uploadPath.endsWith("/")) {
                uploadPath += "/";
            }
            uploadFileDirPrefix = "/" + uploadFileDirPrefix + "/";
            uploadFileDirPrefix = uploadFileDirPrefix.replaceAll("/+", "/");
            uploadPath += uploadFileDirPrefix;
            uploadPath = uploadPath.replaceAll("/+", "/");
            registry.addResourceHandler(uploadFileDirPrefix + "**").addResourceLocations("file:" + uploadPath);
        }
    }

}
